package com.cl.park.action;

import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;

import com.cl.park.model.AjaxData;
import com.cl.park.model.AjaxResponse;
import com.cl.park.model.Car;
import com.cl.park.model.Entrance;
import com.cl.park.model.Floor;
import com.cl.park.model.Park;
import com.cl.park.model.Vip;
import com.cl.park.service.AdminService;
import com.cl.park.service.VpService;
import com.opensymphony.xwork2.ActionSupport;

@Data
@EqualsAndHashCode(callSuper=false)
public class OperatorAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private AjaxData<Object> data;
	private AjaxResponse result;
	private Entrance entrance;
	private Floor floor;
	private Park park;
	private Integer id;
	private String param;
	private VpService vp;
	private Vip vip;
	private Car car;
	private List<Object> list;
	private AdminService adminService;
	/**
	 * 获得会员信息
	 * @return
	 */
	public String editVip(){
		if(null != id){
			vip = (Vip) vp.getOne("Vip", id);
		}
		return SUCCESS;
	}
	
	/**
	 * 添加修改会员
	 * @return
	 */
	public String saveVip(){
		String res = "";
		if(vip.getId()==null){
			res = this.vp.addVIP(vip);
		}else{
			res = this.vp.updateVip(vip);
		}
		if(res.equals("ok")){
			result = new AjaxResponse(true, "操作成功");
		}else{
			result = new AjaxResponse(false, "操作失败");
		}
		return SUCCESS;
	}

	public String index(){
		return SUCCESS;
	}
	
	public String parking(){
		return SUCCESS;
	}
	
	public String taking(){
		list = adminService.entranceList();
		return SUCCESS;
	}

	public String getParkList(){
		data = new AjaxData<Object>(0, 0, adminService.parkList(park));
		return SUCCESS;
	}
	
	public String editPark(){
		list = adminService.entranceList();
		park = adminService.getParkById(id);
		return SUCCESS;
	}
	
	public String getVipList(){
		data = new AjaxData<Object>(0, 0, this.vp.listVip(vip));
		return SUCCESS;
	}

	/**
	 * 会员列表
	 * @return
	 */
	public String listVip(){
		data = new AjaxData<Object>(0, 0, this.vp.listVip(vip));
		return SUCCESS;
	}
	/**
	 * 车位列表
	 * @return
	 */
	public String listPark(){
		return SUCCESS;
	}

	
	
	/**
	 * 停车
	 * @return
	 */
	public String park(){
		String res = this.vp.park(park, id, param);
		if(res.equals("nonw")){
			result = new AjaxResponse(false, "请先添加车辆");
		}
		if(res.equals("ok")){
			result = new AjaxResponse(true, "操作成功");
		}else{
			result = new AjaxResponse(false, "操作失败");
		}
		
		return SUCCESS;
	}
	/**
	 * 离开停车场
	 * @return
	 */
	public String leave(){
		//id 为出入口ID
		String res = this.vp.leave(park, id);
		if(res.equals("no")){
			result = new AjaxResponse(false, "余额不足");
		}else{
			result = new AjaxResponse(true, res);
		}
		return SUCCESS;
	}
	
}
